package com.yzcrm.erp.eqxiu.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.yzcrm.common.constant.BaseConstant;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.utils.AuthUtil;
import com.yzcrm.utils.BaseUtil;
import com.yzcrm.utils.R;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class EqxiuInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv){

        // 跨域
        inv.getController().getResponse().addHeader("Access-Control-Allow-Origin", "*");

        Controller controller = inv.getController();
        HttpServletRequest request = controller.getRequest();
        String requestURI = request.getRequestURI();
        String[] split = requestURI.split("/");
        Long userId = BaseUtil.getUserId();
        boolean flag = false;
        Map<String,String> tablePara =  AuthUtil.getCrmTablePara(CrmEnum.parse(split[1]));
        if(! userId.equals(BaseConstant.SUPER_ADMIN_USER_ID)){
            if(tablePara != null){
                if("addOrUpdate".equals(split[2]) || "saveAndUpdate".equals(split[2])){
                    String rawData = controller.getRawData();
                    JSONObject jsonObject = JSON.parseObject(rawData);

                }else if("deleteByIds".equals(split[2])){
                }else if("queryById".equals(split[2])){
                }
                if(flag){
                    if("queryById".equals(split[2])){
                        controller.renderJson(R.ok().put("data",new Record().set("dataAuth",0)));
                        return ;
                    }else {
                        controller.renderJson(R.error("无权操作"));
                        return;
                    }

                }
            }
        }
        inv.invoke();
    }
}
